<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信息" prop="context">
        <el-input
            v-model="queryParams.context"
            placeholder="请输入信息"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['web:ServeTemplate:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['web:ServeTemplate:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['web:ServeTemplate:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ServeTemplateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="分类" align="center" prop="classify"/>
      <el-table-column label="信息" align="center" prop="context"/>
      <el-table-column label="" align="center" prop="id" v-if="false"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['web:ServeTemplate:edit']"
          >修改
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['web:ServeTemplate:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改业务模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="ServeTemplateRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name" style="width: 350px">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="分类" prop="classify">
          <el-select v-model="chose" placeholder="请选择">
            <el-option v-for="item in classifyMode" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信息" prop="context">
          <!--          <el-input v-model="form.context" type="textarea" placeholder="请输入内容"/>-->
          <el-transfer v-model="form.context" :data="dataSourse"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ServeTemplate">
import {
  listServeTemplate,
  getServeTemplate,
  delServeTemplate,
  addServeTemplate,
  updateServeTemplate,
  ServeClassify
} from "@/api/web/ServeTemplate";

import Editor from "../../../components/Editor";

export default {
  name: '',
  data() {
    return {
      ServeTemplateList: [],
      open: false,
      buttonLoading: false,
      loading: true,
      showSearch: true,
      ids: [],
      chose: undefined,
      single: true,
      multiple: true,
      total: 0,
      title: '',
      classifyMode: [],
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        classify: undefined,
        context: undefined,
      },
      rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        classify: [
          {required: true, message: "分类不能为空", trigger: "blur"}
        ],
        context: [
          {required: true, message: "信息不能为空", trigger: "blur"}
        ],
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
      },
      dataSourse: []
    }
  },
  watch: {
    chose(val) {
      console.log(value)
    }
  },
  created() {
    this.classifList() // 获取业务分类
    this.getList();
  },
  methods: {
    /** 查询业务模板列表 */
    getList() {
      this.loading = true;
      listServeTemplate(this.queryParams).then(response => {
        this.ServeTemplateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        classify: null,
        context: null,
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("ServeTemplateRef");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryRef");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业务模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset();
      const id = row.id || this.ids
      getServeTemplate(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改业务模板";
      });
    },
    classifList () {
      ServeClassify().then(async res => {
        this.classifyMode = res.data
      })
    },
    /** 提交按钮 */
    submitForm() {
      // 数据类型转换
      const data = this.form.context
      data.join(',');
      this.form.context = data
      this.$refs["ServeTemplateRef"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateServeTemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addServeTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除业务模板编号为"' + ids + '"的数据项？').then(function () {
        this.loading = true;
        return delServeTemplate(ids);
      }).then(() => {
        this.loading = true;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('web/ServeTemplate/export', {
        ...this.queryParams
      }, `ServeTemplate_${new Date().getTime()}.xlsx`)
    }
  }
}

</script>
